<?php

namespace app\store\controller\goods;
use app\store\model\Category as CategoryModel;
use app\store\controller\Controller;
use think\Db;
class House extends Controller{

    public function index(){
        $list=Db::name('house')->select()->each(function($item,$key){
        $item['parent_id']=$this->getcate($item['parent_id']);
          return $item;
        });
        return $this->fetch('',compact('list'));
    }

  //片区
  public function getcate($id){
    $list=Db::name('category')->where('category_id',$id)->find();
    return $list['name'];
  }

    public function delete($id) {
        $res=Db::name('house')->delete($id);
        if ($res){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }



    public function add(){
       $model = new CategoryModel;
       $list = $model->getCacheTree();
        if (request()->isPost()){
            $data=request()->param();
            $res=Db::name('house')->insert($data);
            if ($res){
                $this->success('添加成功');
            }else{
                $this->error('添加失败');
            }
        }

        return $this->fetch('',compact('list'));
    }


    public function edit()  {
        $id=input('id');
        if (request()->isPost()){
            $data=request()->param();
            $res=Db::name('house')->where('id',$id)->update($data);
            if ($res){
                $this->success('更新成功');
            }else{
                $this->error('无数据更新');
            }
        }else{
           $model = new CategoryModel;
           $list = $model->getCacheTree();   
          
            $model=Db::name('house')->find($id);
            return $this->fetch('',compact('model','list'));
        }
    }

}
